﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; 

namespace Sinacor.Infra.Service.Validation
{
    /// <summary>
    /// Atributo para controlar tamanho de string
    /// </summary>
    public sealed class StringLength : SinacorBaseValidatorAttribute 
    {

        /// <summary>
        /// Tamanho mínimo
        /// </summary>
        private int _lowBoundary;

        /// <summary>
        /// Tamanho máximo
        /// </summary>
        private int _highBoundary;

        /// <summary>
        /// Construtor, sem ruleset
        /// </summary>
        /// <param name="codException">Código de exceção a ser gerada se propriedade que recebe 
        /// este atributo for inválida</param>
        /// <param name="lowBoundary">Tamanho mínimo</param>
        /// <param name="highBoundary">Tamanho máximo</param>
        public StringLength(int codException, int lowBoundary, int highBoundary) 
            : base(codException)
        {
            _lowBoundary = lowBoundary;
            _highBoundary = highBoundary;
        }

        /// <summary>
        /// Construtor, com ruleset
        /// </summary>
        /// <param name="codException">Código de exceção a ser gerada se propriedade que recebe 
        /// este atributo for inválida</param>
        /// <param name="lowBoundary">Tamanho mínimo</param>
        /// <param name="highBoundary">Tamanho máximo</param>
        public StringLength(int codException, string ruleset, int lowBoundary, int highBoundary) 
            : base(codException, ruleset)
        {
            _lowBoundary = lowBoundary;
            _highBoundary = highBoundary;
        }

        /// <summary>
        /// Método de implementação requerida (abstract na classe base) que retorna 
        /// o objeto Validator a ser utilizado nesta validação
        /// </summary>
        /// <returns></returns>
        public override Microsoft.Practices.EnterpriseLibrary.Validation.Validator CreateValidator()
        {
            return new StringLengthValidator(_lowBoundary, RangeBoundaryType.Inclusive, _highBoundary, RangeBoundaryType.Inclusive);   
        }
    }
}
